﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DeliveryController.cs" company="Oliver Kurowski">
//    (c) Oliver Kurowski. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace RegioKistl.Controllers
{
    using System;
    using System.Globalization;
    using System.Linq;
    using System.Web.Mvc;

    using RegioKistl.Models;
    using RegioKistl.Models.DataAccess;

    public class DeliveryController : Controller
    {
        //
        // GET: /Delivery/

        public ActionResult Index()
        {
            using (var session = Repository.OpenSession())
            {
                var deliveries =
                    session.Query<Models.Delivery>()
                           .Customize(x => x.WaitForNonStaleResults(TimeSpan.FromSeconds(5)))
                           .ToList();

                return this.View(deliveries);
            }
        }

        //
        // GET: /Delivery/Details/5

        public ActionResult Details(int id = 0)
        {
            using (var session = Repository.OpenSession())
            {
                var delivery = session.Load<Models.Delivery>(id);

                if (delivery == null)
                {
                    return this.HttpNotFound();
                }

                return this.View(delivery);
            }
        }

        //
        // GET: /Delivery/Create

        public ActionResult Create()
        {
            using (var session = Repository.OpenSession())
            {
                var allCustomers = (from c in session.Query<Customer>()
                                    where !string.IsNullOrEmpty(c.EmailAddress)
                                    select c).ToList();

                var allCustomersSelectList = (from c in allCustomers
                                              select new SelectListItem
                                                         {
                                                             Selected = false,
                                                             Value = c.Id.ToString(CultureInfo.InvariantCulture),
                                                             Text = string.Format("{0}, {1} ({2})", c.Name, c.FirstName, c.AccountName)
                                                         }).ToList();

                this.ViewBag.AllCustomers = allCustomersSelectList;

                return this.View();
            }
        }

        //
        // POST: /Delivery/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Models.Delivery delivery)
        {
            if (this.ModelState.IsValid)
            {
                using (var session = Repository.OpenSession())
                {
                    session.Store(delivery);
                    session.SaveChanges();
                }

                return RedirectToAction("Index");
            }

            return this.View(delivery);
        }

        //
        // GET: /Delivery/Edit/5

        public ActionResult Edit(int id = 0)
        {
            using (var session = Repository.OpenSession())
            {
                var delivery = session.Load<Models.Delivery>(id);

                if (delivery == null)
                {
                    return this.HttpNotFound();
                }

                return View(delivery);
            }
        }

        //
        // POST: /Delivery/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Models.Delivery delivery)
        {
            if (this.ModelState.IsValid)
            {
                using (var session = Repository.OpenSession())
                {
                    session.Store(delivery);
                    session.SaveChanges();
                }

                return RedirectToAction("Index");
            }

            return this.View(delivery);
        }

        //
        // GET: /Delivery/Delete/5

        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        ////
        //// POST: /Delivery/Delete/5

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Delete(int id, FormCollection collection)
        //{
        //    try
        //    {
        //        // TODO: Add delete logic here

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View();
        //    }
        //}
    }
}
